﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RM.WebClient.RMServices;
using System.IO;
using System.Windows.Browser;
namespace RM.WebClient
{
    public partial class CandidateList : Page
    {
        SaveFileDialog _asyncSaveDialog = new SaveFileDialog();
        public CandidateList()
        {            
            InitializeComponent();            
        }

        void candidateServices_GetAllCompleted(object sender, Candidate_GetAllCompletedEventArgs e)
        {
            try
            {
                this.dataGrid.DataContext = e.Result;
                lblWait.Visibility = Visibility.Collapsed;
                progressBar.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            {
            }
        }

        private void Link_CandidateDetail_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            gridCandidateList.Children.Clear();
            gridCandidateList.Children.Add(new CandidateDetail());    

        }

        private void autoCompleteNameSearch_TextChanged(object sender, RoutedEventArgs e)
        {
            CandidateServiceClient candidateServices = new CandidateServiceClient();
            String sTextContents = autoCompleteNameSearch.Text;
            candidateServices.Candidate_GetAllAsync(sTextContents);
            candidateServices.Candidate_GetAllBySearchCompleted += new EventHandler<Candidate_GetAllBySearchCompletedEventArgs>(candidateServices_GetAllBySearchCompleted);
        }

        void candidateServices_GetAllBySearchCompleted(object sender, Candidate_GetAllBySearchCompletedEventArgs e)
        {
            dataGrid.ItemsSource = e.Result;   
        }

        private void reArrangeDataGridColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            switch (e.Column.Header.ToString())
            { 
                case "FirstName":
                case "First Name":
                    e.Column.DisplayIndex = 0;
                    break;

                case "LastName":
                case "Last Name":
                    e.Column.DisplayIndex = 1;
                    break;

                case "DoB":
                    e.Column.DisplayIndex = 2;
                    break;

                case "Sex":
                    e.Column.DisplayIndex = 3;
                    break;

                case "EmailAddress":
                case "Email Address":
                    e.Column.DisplayIndex = 4;
                    break;

                case "CurrentLocation":
                case "Current Location":
                    e.Column.DisplayIndex = 5;
                    break;

                case "Country":
                    e.Column.DisplayIndex = 6;
                    break;

                case "MobileNo":
                case "Mobile No":
                    e.Column.DisplayIndex = 7;
                    break;

                //case "AppliedForPost":
                //    e.Column.DisplayIndex = 12;
                //    break;

                //case "AreaCode":
                //    e.Column.DisplayIndex = 13;
                //    break;

                //case "CountryCode":
                //    e.Column.DisplayIndex = 14;
                //    break;
                
                default:
                    break;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                CandidateServiceClient candidateServices = new CandidateServiceClient();
                candidateServices.Candidate_GetAllAsync(candidateServices);
                candidateServices.Candidate_GetAllCompleted += new EventHandler<Candidate_GetAllCompletedEventArgs>(candidateServices_GetAllCompleted);
            }
            catch (Exception)
            {
            }
        }

        private void imgAddUser_MouseLeftButtonUp(object sender, MouseEventArgs e)
        {
            //gridCandidateList.Children.Clear();
            //gridCandidateList.Children.Add(new CandidateDetail());
            //System.Net.WebClient client = new System.Net.WebClient();
            //client.OpenReadAsync(new Uri("http://localhost:49688/downloadzip.ashx"));
            //client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            
            if (!e.Cancelled)
            {
                using (Stream fs = _asyncSaveDialog.OpenFile())
                {
                    int length = Convert.ToInt32(e.Result.Length);
                    byte[] byteResult = new byte[length];
                    e.Result.Read(byteResult, 0, length);
                    fs.Write(byteResult, 0, byteResult.Length);
                    fs.Close();
                }
            }


        }

        private void View_Solution(object sender, RoutedEventArgs e)
        {
            Button btnSolution=sender as Button;
            _asyncSaveDialog.DefaultExt = ".Zip";
            _asyncSaveDialog.Filter = "Zip File|*.Zip";
            bool? open = _asyncSaveDialog.ShowDialog();
            if (open.HasValue && open.Value)
            {
                System.Net.WebClient client = new System.Net.WebClient();
                client.OpenReadAsync(new Uri(HtmlPage.Document.DocumentUri ,"downloadzip.ashx?id=" + btnSolution.Tag));
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            }

        }

        private void linkQuestionBank_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri ("QuestionBank",UriKind.Relative));
        }

        private void linkTestSearch_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("TestSearch", UriKind.Relative));
        }       
    }
}
